﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.AlterManagement;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.Account;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IAlterBillService))]
    internal class AlterBillService : IAlterBillService
    {
        [Import]
        protected IAlterBillRepository AlterBillRepository { get; set; }

        public List<AlterBillView> GetAlterBillList(Guid alterID)
        {
            IQueryable<AlterBill> alterBills = AlterBillRepository.Entities.Where(a => a.PID == alterID);
            List<AlterBillView> alterBillViewList = new List<AlterBillView>();
            foreach (AlterBill alterBill in alterBills)
            {
                alterBillViewList.Add(new AlterBillView()
                {
                    ID = alterBill.ID,
                    PID = alterBill.PID,
                    BillCode = alterBill.BillCode,
                    BillName = alterBill.BillName,
                    Unit = alterBill.Unit,
                    OriginalCount = alterBill.OriginalCount,
                    AfterAlterCount = alterBill.AfterAlterCount,
                    AlterCount = alterBill.AlterCount,
                    OriginalPrice = alterBill.OriginalPrice,
                    PresentPrice = alterBill.PresentPrice,
                    OriginalMoney = alterBill.OriginalMoney,
                    PresentMoney = alterBill.PresentMoney,
                    AlterMoney = alterBill.AlterMoney,
                    Remark = alterBill.Remark,
                    LastUpdateUserDepartment=alterBill.LastUpdateUserDepartment,
                    LastUpdateUser=alterBill.LastUpdateUser,
                    LastUpdateTime = alterBill.LastUpdateTime
                });
            }
            return alterBillViewList;
        }

        public void SaveAlterBill(List<AlterBillView> saveList)
        {
            DeleteAlterBill(saveList);
            foreach (AlterBillView alterBillView in saveList)
            {
                AlterBill alterBill = AlterBillRepository.Entities.SingleOrDefault(a => a.ID == alterBillView.ID);
                if (alterBill == null)
                {
                    AddAlterBill(alterBillView);
                }
                else
                {
                    UpdateAlterBill(alterBill, alterBillView);
                }
            }
        }

        private void AddAlterBill(AlterBillView alterBillView)
        {
            AlterBill alterBill = new AlterBill();
            alterBill.ID = alterBillView.ID;
            alterBill.PID = alterBillView.PID;
            alterBill.BillCode = alterBillView.BillCode;
            alterBill.BillName = alterBillView.BillName;
            alterBill.Unit = alterBillView.Unit;
            alterBill.OriginalCount = alterBillView.OriginalCount;
            alterBill.AfterAlterCount = alterBillView.AfterAlterCount;
            alterBill.AlterCount = alterBillView.AlterCount;
            alterBill.OriginalPrice = alterBillView.OriginalPrice;
            alterBill.PresentPrice = alterBillView.PresentPrice;
            alterBill.OriginalMoney = alterBillView.OriginalMoney;
            alterBill.PresentMoney = alterBillView.PresentMoney;
            alterBill.AlterMoney = alterBillView.AlterMoney;
            alterBill.Remark = alterBillView.Remark;
            alterBill.LastUpdateUserDepartment = alterBillView.LastUpdateUserDepartment;
            alterBill.LastUpdateUser = alterBillView.LastUpdateUser;
            alterBill.LastUpdateTime = alterBillView.LastUpdateTime;
            AlterBillRepository.Insert(alterBill);
        }

        private void UpdateAlterBill(AlterBill alterBill,AlterBillView alterBillView)
        {
            alterBill.BillCode = alterBillView.BillCode;
            alterBill.BillName = alterBillView.BillName;
            alterBill.Unit = alterBillView.Unit;
            alterBill.OriginalCount = alterBillView.OriginalCount;
            alterBill.AfterAlterCount = alterBillView.AfterAlterCount;
            alterBill.AlterCount = alterBillView.AlterCount;
            alterBill.OriginalPrice = alterBillView.OriginalPrice;
            alterBill.PresentPrice = alterBillView.PresentPrice;
            alterBill.OriginalMoney = alterBillView.OriginalMoney;
            alterBill.PresentMoney = alterBillView.PresentMoney;
            alterBill.AlterMoney = alterBillView.AlterMoney;
            alterBill.Remark = alterBillView.Remark;
            alterBill.LastUpdateUserDepartment = alterBillView.LastUpdateUserDepartment;
            alterBill.LastUpdateUser = alterBillView.LastUpdateUser;
            alterBill.LastUpdateTime = alterBillView.LastUpdateTime;
            AlterBillRepository.Update(alterBill);
        }

        private void DeleteAlterBill(List<AlterBillView> saveList)
        {
            Guid pid=saveList[0].PID;
            IQueryable<AlterBill> alterBills = AlterBillRepository.Entities.Where(a => a.PID == pid);
            List<Guid> deleteIDList = new List<Guid>();
            foreach (AlterBill alterBill in alterBills)
            {
                AlterBillView exists= saveList.SingleOrDefault(a=>a.ID==alterBill.ID);
                if (exists == null)
                {
                    deleteIDList.Add(alterBill.ID);
                }
            }
            foreach (Guid id in deleteIDList)
            {
                AlterBillRepository.Delete(id);
            }
        }
    }
}